<?php

class EmailController
{
    public function init ()
    {
    }

    public function index ()
    {
        echo 'Welcome!';
    }

    public function equipmentnotify ()
    {
        $model_equipment        = new Model_Equipment();
        $model_items            = new Model_Items();
        $model_project          = new Model_Project();
        
        $equipment_list     = $model_equipment->getNotifyData();
        if($equipment_list){
            foreach($equipment_list as $key => $row){
                $items_list     = $model_items->getDataNotifyByEquipmentId($row['equipmentId']);
                $project_list   = $model_project->getDataByEquipmentId($row['equipmentId']);
                $equipment_list[$key]['items']      = $items_list;
                $arr_temp = array();
                foreach($project_list as $project){
                    $arr_temp[] = $project['projectName'];
                }
                $equipment_list[$key]['project']    = $arr_temp;
            }
            //echo '<pre>',print_r($equipment_list),'</pre>';exit;
            $content = 'Hệ thống tìm thấy một số thiết bị sắp chuyển qua giai đoạn thuê chỗ / sắp hết hạn. <br />
                        Bạn vui lòng kiểm tra và xác nhận lại tại <a href="https://cmdb.ssgroup.com.vn/equipment/notify">trang quản lý của cmdb</a><br /><br /><br />';
            $content .= '
                        <table border="0" cellspacing="0" cellpadding="5" style="border-top: 1px solid #65a000;border-left: 1px solid #d4d4d4;">
                            <tr>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>STT</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Tên thiết bị</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Mã thiết bị</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Người quản lý</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Danh mục</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Dự án</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Items</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Ngày hết hạn</center></th>
                            </tr>
                        ';
            $stt = 1;
            foreach($equipment_list as $row){
                $content .= '<tr>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;"><center>'.$stt.'</center></td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.$row['equipmentName'].'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.$row['code'].'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.$row['username'].'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.$row['categoryName'].'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.str_replace(array('[',']',',','"'),array('','',', ',''),json_encode($row['project'])).'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">
                                <table border="0" cellspacing="0" cellpadding="5" width="100%">
                            ';
                $stt2 = 1;
                foreach($row['items'] as $item){
                    $str = '';
                    if($stt2 > 1)
                        $str = 'border-top: 1px solid #d4d4d4;';
                    $content .= '<tr><td style="'.$str.'">'.$item['itemName'].'</td></tr>';
                    ++$stt2;
                }
                $content .= '    
                                </table>
                            </td>
                            ';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">
                                <table border="0" cellspacing="0" cellpadding="5" width="100%">
                            ';
                $stt2 = 1;
                foreach($row['items'] as $item){
                    $str = '';
                    if($stt2 > 1)
                        $str = 'border-top: 1px solid #d4d4d4;';
                    $content .= '<tr><td style="'.$str.'"><center>'.$this->dataToDate($item['duration']).'</center></td></tr>';
                    ++$stt2;
                }
                $content .= '    
                                </table>
                            </td>
                            </tr>
                            ';
                ++$stt;
            }
            $content .= '
                        </table>
                    ';
            //echo $content;
            
            $list_email = array(
                            'anhhv'         => 'anhhv@ssgroup.com.vn',
                            'phultv'        => 'phultv@ssgroup.com.vn',
                            'tunh'          => 'tunh@ssgroup.com.vn',
                            'huynt'         => 'huynt@ssgroup.com.vn',
                            'vanht'         => 'vanht@ssgroup.com.vn',
                            'donbt'         => 'donbt@ssgroup.com.vn',
                            'minhnv'        => 'minhnv@ssgroup.com.vn',
                            'annm'          => 'annm@ssgroup.com.vn',
                            'hangntt'       => 'hangntt@ssgroup.com.vn',
							'hoaittt'       => 'hoaittt@ssgroup.com.vn',
							'leht'        	=> 'leht@ssgroup.com.vn'							
                        );
                        
                                
            //$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $settings);
            $email_from = "cmdb@ssgroup.com.vn";
            $name_from = "cmdb";

            $mail = new Zend_Mail('UTF-8');
            $mail->setReplyTo($email_from, $name_from);
            $mail->setFrom($email_from, $name_from);
            foreach($list_email as $key=>$value){
                $mail->addTo($value, $key);
            }
            $mail->setSubject('CMDB - Thông báo');
            $mail->setBodyHtml($content);
            try
            {
                $mail->send();//$mail->send($transport);
                echo "Email equipment notify send successfully";
            }
            catch(Zend_Mail_Exception $e)
            {
                echo $e->getMessage();
            }
        }else{
            echo "No equipment notify";
        }
    }

    public function equipmenttrack ()
    {
        $model_equipment        = new Model_Equipment();
        $model_items            = new Model_Items();
        $model_project          = new Model_Project();
        
        $equipment_list     = $model_equipment->getTrackData();
        if($equipment_list){
            foreach($equipment_list as $key => $row){
                $items_list     = $model_items->getDataByEquipmentId($row['equipmentId']);
                $project_list   = $model_project->getDataByEquipmentId($row['equipmentId']);
                $equipment_list[$key]['items']      = $items_list;
                $arr_temp = array();
                foreach($project_list as $project){
                    $arr_temp[] = $project['projectName'];
                }
                $equipment_list[$key]['project']    = $arr_temp;
            }
            //echo '<pre>',print_r($equipment_list),'</pre>';exit;
            $content = 'Hệ thống tìm thấy một số thiết bị được đánh dấu theo dõi nhưng chưa được cập nhật. <br />
                        Bạn vui lòng kiểm tra và xác nhận lại tại <a href="https://cmdb.ssgroup.com.vn">trang quản lý của cmdb</a><br /><br /><br />';
            $content .= '
                        <table border="0" cellspacing="0" cellpadding="5" style="border-top: 1px solid #65a000;border-left: 1px solid #d4d4d4;">
                            <tr>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>STT</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Tên thiết bị</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Mã thiết bị</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Items</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Người quản lý</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Danh mục</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Dự án</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Hạn theo dõi</center></th>
                                <th style="border-right: 1px solid #65a000;border-bottom: 1px solid #65a000;background: #90C140;color: #ffffff;"><center>Nội dung theo dõi</center></th>
                            </tr>
                        ';
            $stt = 1;
            foreach($equipment_list as $row){
                $content .= '<tr>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;"><center>'.$stt.'</center></td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.$row['equipmentName'].'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.$row['code'].'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">
                                <table border="0" cellspacing="0" cellpadding="5" width="100%">
                            ';
                $stt2 = 1;
                foreach($row['items'] as $item){
                    $str = '';
                    if($stt2 > 1)
                        $str = 'border-top: 1px solid #d4d4d4;';
                    $content .= '<tr><td style="'.$str.'">'.$item['itemName'].'</td></tr>';
                    ++$stt2;
                }
                $content .= '    
                                </table>
                            </td>
                            ';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.$row['username'].'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.$row['categoryName'].'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;">'.str_replace(array('[',']',',','"'),array('','',', ',''),json_encode($row['project'])).'</td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;"><center>'.$this->dataToDate($row['trackDate']).'</center></td>';
                $content .= '<td style="border-right: 1px solid #d4d4d4;border-bottom: 1px solid #d4d4d4;"><center>'.$row['trackContent'].'</center></td>';
                ++$stt;
            }
            $content .= '
                        </table>
                    ';
            //echo $content;
                                
            $list_email = array(
                            'anhhv'         => 'anhhv@ssgroup.com.vn',
                            'phultv'        => 'phultv@ssgroup.com.vn',
                            'tunh'          => 'tunh@ssgroup.com.vn',
                            'huynt'         => 'huynt@ssgroup.com.vn',
                            'vanht'         => 'vanht@ssgroup.com.vn',
                            'minhnv'        => 'minhnv@ssgroup.com.vn',
                            'tiendv'        => 'tiendv@ssgroup.com.vn'
                        );
                        
            //$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $settings);
            $email_from = "cmdb@ssgroup.com.vn";
            $name_from = "cmdb";

            $mail = new Zend_Mail('UTF-8');
            $mail->setReplyTo($email_from, $name_from);
            $mail->setFrom($email_from, $name_from);
            foreach($list_email as $key=>$value){
                $mail->addTo($value, $key);
            }
            $mail->setSubject('CMDB - Thông báo');
            $mail->setBodyHtml($content);
            try
            {
                $mail->send();//$mail->send($transport);
                echo "<br />Email equipment tracking send successfully";
            }
            catch(Zend_Mail_Exception $e)
            {
                echo $e->getMessage();
            }
        }else{
            echo "<br />No equipment track";
        }
    }
    public function dataToDate($Date = "0000-00-00") //yyyy-mm-dd to dd/mm/yyyy
    {
        list($y, $m, $d) = preg_split('/-/', $Date);
        return sprintf('%02d/%02d/%4d', $d, $m, $y);
    }
}





